const Joi = require('joi')
const AGVApi = require('../utils/agv/index')

class CreateTask {
	async main(ctx) {
		const schema = Joi.object({
			taskCode     : Joi.any(),
			taskType     : Joi.string(),
			// containerCode : Joi.string().required(),
			locationFrom : Joi.string().required(),
			locationTo   : Joi.string().required(),
			parameters   : Joi.object().unknown(true)
				.allow(null)
		}).unknown(true)

		const { error, value } = schema.validate(ctx.request.body)

		if (error) {
			logger.error(error)
			throw error
		}

		const { taskCode, taskType, locationFrom, locationTo } = value
    
		const positionCodePath = [
			{
				positionCode : locationFrom,
				type         : '00'
			},
			{
				positionCode : locationTo,
				type         : '00'
			}
		]
		const agvCreateTaskContent = {
			reqCode          : taskCode,
			taskCode         : taskCode,
			taskTyp          : 'A01',
			positionCodePath : positionCodePath
		}

		console.log(agvCreateTaskContent)

		const response = await AGVApi.createTask(agvCreateTaskContent)

		console.log(response)

		if (response.code !== '0') {
			logger.debug('请求AGV创建任务接口返回内容非0')

			return ctx.body = {
				responseCode    : 500,
				responseMessage : response.message,
				parameters      : value.parameters || {}
			}
		}
		else {
			return ctx.body = {
				responseCode    : 0,
				responseMessage : '',
				parameters      : value.parameters || {}
			}
		}

	}
}

module.exports = new CreateTask()